<template>
	<view class="contaier">
		<u-navbar title="我的课程" is-fixed
			background="url('https://www.yplm888.com/uploads/20220803/7ed305f9a93ba50b049a7725662b18cc.png') no-repeat">
		</u-navbar>
		<scroll-view class="" :scroll-y="true" :style="'height:' + scrollH+'rpx;'" @scrolltolower="lowerBottom"
			refresher-enabled="true" :refresher-triggered="triggered" :refresher-threshold="50"
			@refresherpulling="onPulling" @refresherrefresh="onRefresh" @refresherrestore="onRestore"
			@refresherabort="onAbort">
			<view class="main" v-if="myClassesList.length>0">
				<class-list :dataList="myClassesList"></class-list>
			</view>
			<view style="margin-top: 100rpx;" v-else>
				<no-thing></no-thing>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import classList from '../components/class-list.vue'
	export default {
		components: {
			classList
		},
		data() {
			return {
				myClassesList: [],
				page: 1,
				scrollH: 0,
				triggered: false,
			}
		},
		onLoad() {
			this._freshing = false;
			this.initData()
		},
		onReady() {
			let that = this;
			uni.getSystemInfo({
				success: function(res) {
					that.scrollH = res.windowHeight * 750 / res.windowWidth;
					// #ifdef MP-WEIXIN
					that.scrollH = res.windowHeight * 750 / res.windowWidth - parseInt(res.safeArea.top) - 200;
					// #endif
				}
			});
		},
		onReachBottom() {
			this.page++
			this.initData()
		},
		methods: {
			initData() {
				let data = {
					page: this.page,
					limit: 15
				}
				this.$u.api.myClassesList(data).then(res => {
					this.myClassesList = [...this.myClassesList, ...res.data]
				})
			},
			onPulling(e) {
				this.triggered = true;
			},
			async onRefresh() {
				if (this._freshing) return;
				this._freshing = true;
				this.page = 1;
				await setTimeout(() => {
					this.triggered = false;
					this._freshing = false;
					this.myClassesList = []
					this.initData();
				}, 1000);
				uni.showToast({
					title: '刷新成功',
					duration: 1000
				})
			},
			onRestore() {
				this.triggered = false; // 需要重置
			},
			onAbort() {},
			// 触底加载
			lowerBottom() {
				this.page++;
				this.initData();
			}
		}
	}
</script>

<style>
	.contaier {
		padding: 0 20rpx;
	}

	.main {
		margin: 0 auto;
	}
</style>